//引入路径模块
const path = require("path")
//引入html打包模块
const HtmlWebpackPlugin = require('html-webpack-plugin');
//打包公共资源模块
const CopyWebpackPlugin = require("copy-webpack-plugin")

module.exports = {
    //入口
    entry: path.resolve(__dirname, "../src/index.js"),

    //出口
    output: {
        //出口路径
        path: path.resolve(__dirname, "../build"),
        //出口文件名
        filename: "./js/main.js",
        publicPath: "/"
    },

    //loader配置
    module: {
        // html-loader配置
        /* rules: [{
            test: /\.html$/i,
            use: [
                'html-loader'
            ]
        }],*/
        rules: [
            //......
            {
                test: /\.ejs$/,
                loader: 'ejs-loader',
                options: {
                    variable: 'data', // 可以在模块当中使用data进行数据处理
                }
            },
            // css配置
            {
                test: /\.css$/i,
                use: [
                    //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                    "style-loader",
                    'css-loader'
                ]
            }
        ],
    },

    //plugins插件配置
    plugins: [
        //html打包插件配置
        new HtmlWebpackPlugin({
            // filename: "1111.html",
            template: path.resolve(__dirname, "../public/index.html"),

        }),
        new CopyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, "../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                }
            }],
        }),
    ],

    resolve: {
        // webpack中配置路径别名
        alias: {
            $v: path.resolve(__dirname, "../src/views"),
            $r: path.resolve(__dirname, "../src/routes"),
            $a: path.resolve(__dirname, "../src/api")
        },

        // 尝试按顺序解析这些后缀名。如果有多个文件有相同的名字，但后缀名不同，webpack 会解析列在数组首位的后缀的文件 并跳过其余的后缀。
        extensions: [".js", ".json", ".ejs"],
    },

    //模式
    mode: "production"
}